package cn.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.pojo.Detail;
import cn.service.DetailService;
import cn.util.DateUtil;

@Controller
@RequestMapping("/detail")
public class detailController {
	@Resource
	private DetailService detailService;

	@RequestMapping("/getDetail")
	private String getDetail(@RequestParam(value = "sortId", defaultValue = "0", required = false) Long sortId,
			HttpServletRequest request, HttpServletResponse response, Model md) {
		List<Detail> list = detailService.getDetailBySortId(sortId);
		md.addAttribute("LIST", list);
		return "bbsList";
	}

	@RequestMapping("/toAddDetail")
	private String toAddDetail() {
		return "addDetail";
	}

	@RequestMapping("/addDetail")
/*	private String addDetail(Detail detail,//
			HttpServletRequest request, HttpServletResponse response, Model md) {*/
	private String addDetail(@RequestParam(value = "sortId", defaultValue = "0", required = false) Long sortId,//
			@RequestParam(value = "author", defaultValue = "0", required = false) String author,//
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "detail", defaultValue = "", required = false) String detail,
			HttpServletRequest request, HttpServletResponse response, Model md) {
		//System.out.println(detail);
		Detail d=new Detail();
		d.setAuthor(author);
		d.setDetail(detail);
		d.setSortId(sortId);
		d.setTitle(title);
		d.setReplyCount(0);
		d.setCreateDate(DateUtil.convertUtilDateToTimeStamp(new Date()));
		int i=detailService.addDetail(d);
		return "redirect:/detail/getDetail";
	}
}
